import pandas as pd
import numpy as np

from sklearn.metrics import r2_score #评价回归预测模型的性能

import matplotlib.pyplot as plt #画图

# 读入数据
train = pd.read_csv("day.csv")

# 数据探索

#对数据值型特征，用常用统计量观察其分布
# train.describe()

# 特征工程

#对类别型特征，观察其取值范围及直方图
categorical_features = ['season','mnth','weathersit','weekday']

#数据类型变为object，才能被get_dummies处理
for col in categorical_features:
    train[col] = train[col].astype('object')
    
X_train_cat = train[categorical_features]
X_train_cat = pd.get_dummies(X_train_cat)
X_train_cat.head()

# 数值型特征
# 对数值型特征进行标准化/MinMaxScaler，去量纲

#数值型变量预处理，
#感觉数据已经做过处理（取值都在0-1之间），这里用MinMaxScaler再处理一次
from sklearn.preprocessing import MinMaxScaler
mn_X = MinMaxScaler()
numerical_features = ['temp','atemp','hum','windspeed']
temp = mn_X.fit_transform(train[numerical_features])

X_train_num = pd.DataFrame(data=temp, columns=numerical_features, index =train.index)
X_train_num.head()

# Join categorical and numerical features
X_train = pd.concat([X_train_cat, X_train_num, train['holiday'],  train['workingday']], axis = 1, ignore_index=False)
X_train.head()

FE_train = pd.concat([train['instant'], X_train,  train['yr'],train['cnt']], axis = 1)
FE_train.to_csv('FE_day.csv', index=False)
FE_train.head()

# FE_train.info()

# 从原始数据中分离输入特征x和输出y
y = FE_train["cnt"]

X = FE_train.drop(["cnt"], axis = 1)

#特征名称，用于后续显示权重系数对应的特征
feat_names = X.columns

#将数据分割训练数据与测试数据
from sklearn.model_selection import train_test_split

# 随机采样20%的数据构建测试样本，其余作为训练样本
X_train, X_test, y_train, y_test = train_test_split(X, y, random_state=33, test_size=0.2)
# print(X_train.shape)

# 线性回归

# 尝试缺省参数的线性回归
#class sklearn.linear_model.LinearRegression(fit_intercept=True, normalize=False, copy_X=True, n_jobs=1)
from sklearn.linear_model import LinearRegression

# 1.使用默认配置初始化学习器实例
lr = LinearRegression()

# 2.用训练数据训练模型参数
lr.fit(X_train, y_train)

# 3. 用训练好的模型对测试集进行预测
y_test_pred_lr = lr.predict(X_test)
y_train_pred_lr = lr.predict(X_train)

# 看看各特征的权重系数，系数的绝对值大小可视为该特征的重要性
fs = pd.DataFrame({"columns":list(feat_names), "coef":list((lr.coef_.T))})
fs.sort_values(by=['coef'],ascending=False)

# 使用r2_score评价模型在测试集和训练集上的性能，并输出评估结果
#测试集
print('The r2 score of LinearRegression on test is', r2_score(y_test, y_test_pred_lr))
# 训练集
print('The r2 score of LinearRegression on train is', r2_score(y_train, y_train_pred_lr))

# 正则化的线性回归（L2正则 --> 岭回归）
from sklearn.linear_model import RidgeCV
#1. 设置超参数（正则参数）范围
alphas = [ 0.01, 0.1, 1, 10,100]
#2. 生成一个RidgeCV实例
ridge = RidgeCV(alphas=alphas, store_cv_values=True)
#3. 模型训练
ridge.fit(X_train, y_train)
#4. 预测
y_test_pred_ridge = ridge.predict(X_test)
y_train_pred_ridge = ridge.predict(X_train)
# 评估，使用r2_score评价模型在测试集和训练集上的性能
print('The r2 score of RidgeCV on test is', r2_score(y_test, y_test_pred_ridge))
print('The r2 score of RidgeCV on train is', r2_score(y_train, y_train_pred_ridge))

# 看看各特征的权重系数，系数的绝对值大小可视为该特征的重要性
fs = pd.DataFrame({"columns":list(feat_names), "coef_lr":list((lr.coef_.T)), "coef_ridge":list((ridge.coef_.T))})
fs.sort_values(by=['coef_lr'],ascending=False)

# 正则化的线性回归（L1正则 --> Lasso）
from sklearn.linear_model import LassoCV

#1. 设置超参数搜索范围
#alphas = [ 0.01, 0.1, 1, 10,100]

#2. 生成学习器实例
#lasso = LassoCV(alphas=alphas)

#1. 设置超参数搜索范围
#Lasso可以自动确定最大的alpha，所以另一种设置alpha的方式是设置最小的alpha值（eps） 和 超参数的数目（n_alphas），
#然后LassoCV对最小值和最大值之间在log域上均匀取值n_alphas个
# np.logspace(np.log10(alpha_max * eps), np.log10(alpha_max),num=n_alphas)[::-1]

#2 生成LassoCV实例（默认超参数搜索范围）
lasso = LassoCV()  

#3. 训练（内含CV）
lasso.fit(X_train, y_train)

#4. 测试
y_test_pred_lasso = lasso.predict(X_test)
y_train_pred_lasso = lasso.predict(X_train)

# 评估，使用r2_score评价模型在测试集和训练集上的性能
print('The r2 score of LassoCV on test is', r2_score(y_test, y_test_pred_lasso))
print('The r2 score of LassoCV on train is', r2_score(y_train, y_train_pred_lasso))

# 看看各特征的权重系数，系数的绝对值大小可视为该特征的重要性
fs = pd.DataFrame({"columns":list(feat_names), "coef_lr":list((lr.coef_.T)), "coef_ridge":list((ridge.coef_.T)), "coef_lasso":list((lasso.coef_.T))})
fs.sort_values(by=['coef_lr'],ascending=False)